﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using wdss.WebServices;

namespace wdss.GUI
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            username_lbl.Text = (string)Session["username"];
            username_lbl.Visible = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now);
            Response.Cache.SetNoServerCaching();
            Response.Cache.SetNoStore();
            //check for the type of user and direct them to appropriate page
            if ((int)Session["role"] == 2)
            {
                Server.Transfer("~/GUI/DocumentList.aspx");
            }
            if ((int)Session["role"] == 1)
            {
                Server.Transfer("~/GUI/Login.aspx");
            }
            if ((int)Session["role"] == 3)
            {
                Server.Transfer("~/GUI/SystemAdministrator.aspx");
            }
        }
        protected void Upload_Button_Click(object sender, EventArgs e)
        {
            LogService logAction = new LogService();
            String fileName = txt_fileName.Text.Trim();
            FileUpload filePath = FileUploadPath;
            UploadService uploadServiceObj = new UploadService();
            int userid = (int)Session["userid"];
            if (fileName.Length < 1)
            {
                lbl_Success.Visible = false;
                lbl_Error.Visible = true;
                lbl_Error.Text = "No title name.";
            }
            else
            {
                try
                {
                    String returnVal = uploadServiceObj.UploadFileService(fileName, filePath, (int)Session["userid"]);
                    if (returnVal == "Success.")
                    {

                        lbl_Success.Visible = true;
                        lbl_Error.Visible = false;
                        logAction.LogAction("User " + (string)Session["username"] + " successfully uploaded a file.");
                        //UploadCheckService uploadcheck = new UploadCheckService();
                        //uploadcheck.insertIntoTable(userid, fileName, filePath);
                        if ((int)Session["role"] != 6)
                        {
                            CeoService share = new CeoService();
                            share.shareOnUpload(userid, fileName, filePath);
                        } 
                        if ((int)Session["role"] != 6 && (int)Session["role"] != 5)
                        {
                            ManagerService share1 = new ManagerService();
                            share1.shareOnUpload(userid, fileName, filePath);
                        }

                        
                    }

                else
                    {
                        lbl_Success.Visible = false;
                        lbl_Error.Visible = true;
                        lbl_Error.Text = returnVal;
                        logAction.LogAction("User " + (string)Session["username"] + " failed to upload a file due to " + returnVal + ".");
                    }
                }
                catch (HttpException)
                {
                    lbl_Success.Visible = false;
                    lbl_Error.Visible = true;
                    lbl_Error.Text = "No session found.";
                    //no userid means no login, so direct to login page
                    Server.Transfer("~/GUI/Login.aspx");
                }
                
            }
        }
    }

}

